﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BuyLocalBonds
{
    public partial class Buy_Bond : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // set the read-only textboxes to the session values
            CusipTextBox.Text = (string)Session["cusip"];
            RatingTB.Text = (string)Session["rating"];
            YieldTB.Text = (string)Session["current_yield"];
            YtmTB.Text = (string)Session["ytm"];
            CouponTB.Text = (string)Session["coupon"];
            MatDtTB.Text = (string)Session["maturity_date"];
            PriceTB.Text = (string)Session["price"];
            QtyAvailTB.Text = (string)Session["qty_avail"];
        }

        //TODO: update the quantity in the bond table
        protected void BuyButton_Click(object sender, EventArgs e)
        {
            //BuyQuantityFieldValidator.Enabled = true;
            SqlBean sb = new SqlBean();
            string traderID = "1";
            string confirmationPage = "buyconfirmation.aspx";

            // receive values in an array: cusip, rating, price, quantity, purchase amount
            int qty = Int32.Parse(BuyQuantityTextBox.Text);
            int qtyAvail = Int32.Parse(QtyAvailTB.Text);
            bool status = sb.BuyBond(CusipTextBox.Text, qty, CustomerIDTextBox.Text, traderID, qtyAvail);

            if (status == false)
            {
                // quantity exceeded
                MessageLabel.Text = "Unfortunately, the operation failed. Verify the CustomerID and Quantity!";
            }
            else
            {
                Session["quantity"] = BuyQuantityTextBox.Text;
                Session["purchase_amount"] = 0;

                Response.Redirect(confirmationPage);
            }

        }
        protected void BacktoResults_Click(object sender, EventArgs e)
        {
            //BuyQuantityFieldValidator.Enabled = false;
            //ValidationSummary.Enabled = false;
            Response.Redirect("searchResults.aspx");

        }
    }
}